﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for EmployeeBySuperiorIDAdminSerializer
    /// </summary>
    public class EmployeeBySuperiorIDAdminSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                    "ID",
                   "No",
                   "EmployeeName",
                   "SubBranch",
                   "Department",
                   "Section",
                   "SubSection",
                   "JobPosition",
                   "JobTitle",
                   "isActive",
                    "ID2",
			       
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int SuperiorID = Convert.ToInt16(HttpContext.Current.Request.Params["User"]);

            int RgmTypeID = Convert.ToInt16(HttpContext.Current.Request.Params["RgmTypeID"]);
            int RgmID = Convert.ToInt16(HttpContext.Current.Request.Params["RgmID"]);
            int BranchID = Convert.ToInt16(HttpContext.Current.Request.Params["BranchID"]);
            int SubBranchID = Convert.ToInt16(HttpContext.Current.Request.Params["SubBranchID"]);
            int DepartmentID = Convert.ToInt16(HttpContext.Current.Request.Params["DepartmentID"]);
            int SectionID = Convert.ToInt16(HttpContext.Current.Request.Params["SectionID"]);

            Employee Superior = new EmployeeFacade().GetEmployeeByID(SuperiorID);

            IList<Employee> result = new List<Employee>();
            EmployeeFacade pf = new EmployeeFacade();
            int iTotalRecords = 0;
            int iTotalDisplayRecords = 0;
            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();
            sysLogFacade sysLogFacade = new sysLogFacade();
            sysLog objSysLog = new sysLog();
            try
            {
                result = pf.GetUnderlingBySuperiorIDAdmin(RgmTypeID, RgmID, BranchID, SubBranchID, DepartmentID, SectionID, sSearch, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords);
                iTotalDisplayRecords = iTotalRecords;

                if (result.Count > 0)
                {
                    int idx = iDisplayStart;
                    foreach (Employee p in result)
                    {

                        idx++;
                        IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                        ColumnValues.Add(this.Columns[0], p.ID.ToString());
                        ColumnValues.Add(this.Columns[1], idx.ToString());
                        ColumnValues.Add(this.Columns[2], p.FirstName.ToString() + " " + p.LastName.ToString());
                        ColumnValues.Add(this.Columns[3], p.SubBranch == null ? "" : p.SubBranch.Description.ToString());
                        ColumnValues.Add(this.Columns[4], p.Department == null ? "" : p.Department.Description.ToString());
                        ColumnValues.Add(this.Columns[5], p.Section == null ? "" : p.Section.Description.ToString());
                        ColumnValues.Add(this.Columns[6], p.SubSection == null ? "" : p.SubSection.Description.ToString());
                        ColumnValues.Add(this.Columns[7], p.JobPosition == null ? "" : p.JobPosition.Position.ToString());
                        ColumnValues.Add(this.Columns[8], p.JobTitle.ToString());
                        ColumnValues.Add(this.Columns[9], p.isActive.ToString());
                        ColumnValues.Add(this.Columns[10], p.ID.ToString());

                        collection.Add(idx, ColumnValues);
                    }
                }
            }
            catch(Exception ex)
            {
                objSysLog = new sysLog();
                objSysLog.Description = ex.Message;
                objSysLog.LogEvent = "Load Data Grid";
                objSysLog.Source = "EmployeeBySuperiorIDAdminSerializer.ashx";
                objSysLog.CreatedBy = Superior.Email;
                objSysLog.CreatedDate = DateTime.Now;
                sysLogFacade.Save(objSysLog);
            }

            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}